/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package soatonline.VO;

/**
 *
 * @author andresdonato
 */
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class TestConnection {

    public String bd = "soatseguro";
    public String servidor = "localhost";
    public String login = "root";
    public String password = "";
    public String url = "jdbc:mysql://" + servidor + "/" + bd;
    Connection conexion = null;
    String getString;

    public void conectar() {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conexion = DriverManager.getConnection(url, login, password);
            if (conexion != null) {
                System.out.println("Conexión a base de datos " + url + " ... Ok");


            }
        } catch (SQLException ex) {
            System.out.println("Hubo un problema al intentar conectarse con la base de datos " + url);
            System.out.println(ex);
        } catch (ClassNotFoundException ex) {
            System.out.println(ex);
        }
    }

    public ResultSet Consulta(String consulta) throws SQLException {
        this.conectar();
        try {

            Statement stm = conexion.createStatement();

            PreparedStatement pstm = stm.getConnection().prepareStatement(consulta);


            ResultSet res = pstm.executeQuery();
            return res;
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Se encontro en siguiente error: " + e, "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
        



    }

    public int update(String consulta) throws SQLException {
        this.conectar();
        try {
            Statement stm = conexion.createStatement();
            PreparedStatement pstm = stm.getConnection().prepareStatement(consulta);

            int res = pstm.executeUpdate(consulta);
            return res;
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "No se pudo encontrar el codigo de categoria usando hashtable " + e, "Error Categorias", JOptionPane.ERROR_MESSAGE);
            return 0;
        } finally {
        }



    }

    public void cerrar() {
        try {
            this.conexion.close();
            System.out.println("Conexion cerrada...!!");
        } catch (SQLException ex) {
            Logger.getLogger(TestConnection.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
